<?php 
class Main extends MY_Controller {
    
    function __construct() {
        parent::__construct();
        
		$this->load->library('form_validation');
		$this->load->model('society_model', '', TRUE);
        $this->load->model('teacher_model', '', TRUE);
        $this->load->model('user_model', '', TRUE);
    }
    
    /**
     * 社团指导老师后台管理首页
     */
    function index() {
		$user_id = $_SESSION['id'];
		
		$condition['teacher_id'] = $user_id;
		$data['teacher'] = $this->teacher_model->search_by_condition($condition, TRUE);
				
		$condition['s_teacher'] = $user_id;
		$data['society'] = $this->society_model->search_by_condition($condition, TRUE);

		/*echo "<pre>";
		print_r($data['society']);
		return ;*/
		////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		$this->load->view("backend/teacher/index", $data);
    }
    
    /**
     * 社团指导老师基本信息修改
     */ 
	function update_info() {
		$user_id = $_SESSION['id'];
		 
		$condition['teacher_id'] = $user_id;
		$data['teacher'] = $this->teacher_model->search_by_condition($condition, TRUE);
		
		////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		////////////////用户后台侧栏信息绑定/////////////
		$data['sidebar_info'] = $this->sidebar_info();
		
		$this->load->view('backend/teacher/update_info', $data);
    }
    
    function save_new_info() {
    	$this->form_validation->set_rules('t_name', '姓名', 'required|xss_clean');
		$this->form_validation->set_rules('t_gender', '性别', 'required');
		$this->form_validation->set_rules('t_birth', '生日', 'required');
		$this->form_validation->set_rules('t_phone', '联系电话', 'required|xss_clean|numeric');
		$this->form_validation->set_rules('t_qq', 'QQ', 'required|xss_clean|numeric|min_length[4]|max_length[15]');
		
    	if ($this->form_validation->run() == FALSE) {
    		$this->update_info();
    		return;
		}
    	$data = $this->_get_form_array();
    	$data['t_birth'] = $this->common->filter_chs($data['t_birth']);
    	
		$this->teacher_model->update($data);
		redirect(BACKEND_TEACHER_PATH);
    }
 
    
    /**
     * 邮箱修改
     */
    function show_change_email() {
    	
    	////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		////////////////用户后台侧栏信息绑定/////////////
		$data['sidebar_info'] = $this->sidebar_info();
		
    	$this->load->view('backend/teacher/change_email', $data);
    }
    
   	function change_email() {
 		//修改email的常规校验
 		$this->form_validation->set_rules('old_email', '旧邮箱', 'required|email|min_length[4]|max_length[50]');
 		$this->form_validation->set_rules('email', '新邮箱', 'required|email|min_length[4]|max_length[50]');
 		$this->form_validation->set_rules('re_email', '再次填写邮箱', 'required|email|min_length[4]|max_length[50]|matches[email]');
 		//开始验证
	 	if ($this->form_validation->run() == FALSE) {
			$this->show_change_email();
			return;	
	 		}
 		
		//逻辑校验
 		$this->form_validation->set_rules('change_error', '', 'callback_change_email_check');
 		//开始逻辑校验
 		if ($this->form_validation->run() == FALSE) {
			$this->show_change_email();
			return;
		}
		
		//保存数据
 		$t_m = array('old_email', 're_email');
    	$data = $this->_get_form_array($t_m);
    	$id = $_SESSION['id'];
    	$data['id'] = $id;
    	
    	$this->user_model->update($data);
    	session_destroy();
    	$this->session->sess_destroy();
    	redirect(TEACHER_LOGIN_PATH);
 	}
 	
	//email修改  逻辑校验的回调函数  
 	function change_email_check() {
 		$old_email = $this->input->post('old_email');
 		
 		$email = $this->input->post('email');
 		
 		$temp_1 = $this->user_model->get_user_by_email_single($old_email);
 		$temp_2 = $this->user_model->get_user_by_email_single($email);
 		
 		if (!$temp_1) {
 			$this->form_validation->set_message('change_email_check', '原邮箱不存在');
 			return FALSE;
 		}
 		
 		if ($temp_2) {
 		    $this->form_validation->set_message('change_email_check', '新输入的邮箱已存在');
 			return FALSE;
 		}
 	}

 	
 	/**
     * 密码修改
     */
	function show_change_password() {
		
    	////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		////////////////用户后台侧栏信息绑定/////////////
		$data['sidebar_info'] = $this->sidebar_info();
    	
		$this->load->view('backend/teacher/change_password', $data);
    }
    
 	function change_password() {
 		//修改password的常规校验
 		$this->form_validation->set_rules('old_password', '旧密码', 'required|xss_clean|min_length[6]|max_length[16]');
 		$this->form_validation->set_rules('password', '新密码', 'required|xss_clean|min_length[6]|max_length[16]');
 		$this->form_validation->set_rules('re_password', '再次填写密码', 'required|matches[password]|xss_clean');
 		//开始验证
	 	if ($this->form_validation->run() == FALSE) {
			$this->show_change_password();
			return;	
	 		}
 		
		//逻辑校验
 		$this->form_validation->set_rules('change_error', '', 'callback_change_password_check');
 		//开始逻辑校验
 		if ($this->form_validation->run() == FALSE) {
			$this->show_change_password();
			return;
		}
		
		//保存 修改的数据 password
 		$t_m = array('old_password', 're_password');
    	$data = $this->_get_form_array($t_m);
    	$id = $_SESSION['id'];
    	$data['id'] = $id;
    	$this->user_model->update($data);
    	redirect(TEACHER_LOGIN_PATH);
 	}

	//password修改  逻辑校验的回调函数  
	function change_password_check() {
		$data = $this->_get_form_array();
		$id = $_SESSION['id'];
		$temp = $this->user_model->get_by_id($id);
 		if ($temp['password'] != $data['old_password']) {
 			$this->form_validation->set_message('change_password_check', '原密码不存在');
 			return FALSE;
 		}
 	}
}